//
//  CLTableViewCell.m
//  CLTableViewProject
//
//  Created by Charles on 16/5/20.
//  Copyright © 2016年 Charles. All rights reserved.
//

#import "CLTableViewCell.h"

@implementation CLTableViewCell

{
    UIImageView *_arrowImageView;
}
@synthesize item = _item,indexPath = _indexPath;


+ (CGFloat)tableView:(UITableView *)tableView rowHeightForItem:(CLTableItem *)item {
    if (item != nil) {
        return item.rowHeight+0.5;
    }
    return 44;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _arrowRightMargin = 10;
        [self awakeFromNib];
    }
    return self;
}

- (void)setItem:(CLTableItem *)item {
    if (item != _item) {
        if (_item) {
            [self finishObserveObjectProperty];
            _item = nil;
        }
        self.textLabel.text = item.desc;
        _item = item;
        if (_item)
            [self startObserveObjectProperty];
    }
}


#pragma mark Arrow
- (UIImageView *)arrowImageView {
    if (_arrowImageView == nil) {
        UIImage *image = self.arrowImage;
        _arrowImageView = [[UIImageView alloc] initWithImage:image];
        _arrowImageView.highlightedImage = self.arrowHlImage;
        _arrowImageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        _arrowImageView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_arrowImageView];
    }
    return _arrowImageView;
}

- (UIImage *)arrowImage {
    return [UIImage imageNamed:@"arrow_right"];
}

- (UIImage *)arrowHlImage {
    UIImage *image = [UIImage imageNamed:@"arrow_right"];
    if (!image) {
        image = self.arrowImage;
    }
    return image;
}

- (void)setArrowRightMargin:(NSInteger)arrowRightMargin {
    _arrowRightMargin = arrowRightMargin;
    if (_arrowImageView) {
    }
}

- (void)setShowArrowIcon:(BOOL)showArrowIcon {
    _showArrowIcon = showArrowIcon;
    if (showArrowIcon) {
        self.arrowImageView.hidden = NO;
    } else if (_arrowImageView) {
        _arrowImageView.hidden = YES;
    }
}

- (UIImageView *)getArrowImageView{
    return _arrowImageView;
}
#pragma mark Object Property Observer

- (void)startObserveObjectProperty {
    
}

- (void)finishObserveObjectProperty {
    
}

- (void)addObservedProperty:(NSString *)property {
    [_item addObserver:self forKeyPath:property
               options:NSKeyValueObservingOptionNew
               context:nil];
}

- (void)removeObservedProperty:(NSString *)property {
    [_item removeObserver:self forKeyPath:property];
}

- (void)objectPropertyChanged:(NSString *)property {
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object != _item) {
        [object removeObserver:self forKeyPath:keyPath];
    }
    else {
        [self objectPropertyChanged:keyPath];
    }
}

- (void)dealloc {
    if (_item) {
        [self finishObserveObjectProperty];
        _item = nil;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
